1、页面继承
定义base.html:
{% load staticfiles %}.....{% block content %}{% endblock %}
index.html:
{% extends 'base.html' %}{% block title %}在线学习系统--首页 {% endblock %}{% load staticfiles %}{% block content %}........{% endblock %}
2、分页、排序
分页插件:https://github.com/jamespacileo/django-pure-pagination#settings
settings.py:
INSTALLED_APPS = [...... 'pure_pagination', # 注册分页app]
# 分页PAGINATION_SETTINGS = { 'PAGE_RANGE_DISPLAYED': 10, 'MARGIN_PAGES_DISPLAYED': 2, 'SHOW_FIRST_PAGE_WHEN_INVALID': True,}
views.py:
from pure_pagination import Paginator, EmptyPage, PageNotAnInteger
def video_list(request): video_list = Video.objects.all() video_page_count = video_list.count(); # 分页功能 try: page = request.GET.get('page', 1) # 获取当前页码,如果没有默认1 except PageNotAnInteger: # 如果获取页码出错,默认1 page = 1 tmp = Paginator(video_list, 2, request=request) # 执行分页函数,参数1数据库的数据,参数2显示多少条数据,参数3request video_page = tmp.page(page) # 返回一个,包含了分页数据和分页导航的对象 return render(request, "video_list.html", {"video_page": video_page, "video_page_count": video_page_count})
html:
视频列表{% for video in video_page.object_list %} {% endfor %}
{% load i18n %}{% if video_page.has_previous %} ‹‹ {% trans "previous" %} {% else %} ‹‹ {% trans "previous" %} {% endif %} {% for page in video_page.pages %} {% if page %} {% ifequal page video_page.number %} { { page }} {% else %} { { page }} {% endifequal %} {% else %} ... {% endif %} {% endfor %} {% if video_page.has_next %} {% trans "next" %} ›› {% else %} {% trans "next" %} ›› {% endif %}
3、ModelForm提交、验证